﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine.Networking;
using UnityEngine.UI;
using UnityEngine;

public class GameManager : NetworkBehaviour
{
    public int playerNum;
    public Text showIP;
    [SyncVar]
    public float playersMbHp1;
    [SyncVar]
    public float playersMbHp2;
    [SyncVar]
    private bool isStart; //private
    [SyncVar]
    public bool hasWinner;
    [SyncVar]
    private int winnerNum;

    public bool IsStart
    {
        get
        {
            return isStart;
        }
    }

    public int WinnerNum
    {
        get
        {
            return winnerNum;
        }
    }

    private void Start()
    {
        playerNum = 0;
        showIP.text = Network.player.ipAddress;
        isStart = false;
        hasWinner = false;
        playersMbHp1 = 100;
        playersMbHp2 = 100;
    }

    private void Update()
    {
        if (playerNum >= 2)
        {
            if (!hasWinner)
            {
                isStart = true;
                if (playersMbHp1 == 0)
                {
                    CmdSetWinner(1);
                    hasWinner = true;
                }
                else if (playersMbHp2 == 0)
                {
                    CmdSetWinner(0);
                    hasWinner = true;
                }
            }
            else
            {
                isStart = false;
            }
        }
    }

    [Command]
    private void CmdSetWinner(int n)
    {
        hasWinner = true;
        isStart = false;
        winnerNum = n;
    }
}
